/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.permit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.enums.DeletedEnum;
import net.siufung.market.base.permit.mapper.IRoleMapper;
import net.siufung.market.base.permit.mapper.entity.Role;
import net.siufung.market.base.permit.mapper.entity.RolePermit;
import net.siufung.market.base.permit.service.IRolePermitService;
import net.siufung.market.base.permit.service.IRoleService;
import net.siufung.mybatis.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 系统角色表 数据服务实现
 *
 * @author 陈建峰
 * @since 2022-07-05 16:14:50
 */
@Slf4j
@AllArgsConstructor
@Service(value = "roleService")
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl extends BaseServiceImpl<IRoleMapper, Role> implements IRoleService {

    private final IRolePermitService rolePermitService;

    @Override
    public Boolean save(Role role, List<Integer> permitIdList){
        if(CollectionUtils.isEmpty(permitIdList)){
            throw new BizServiceException("权限列表为空！");
        }
        if(super.save(role)){
            RolePermit rolePermit;
            List<RolePermit> rolePermitList = Lists.newArrayList();
            for (Integer permitId: permitIdList) {
                rolePermit = new RolePermit();
                rolePermit.setRoleId(role.getId());
                rolePermit.setPermitId(permitId);
                rolePermit.setTenantId(role.getTenantId());
                rolePermitList.add(rolePermit);
            }
            return this.allocationPermit(rolePermitList);
        }
        return Boolean.FALSE;
    }

    @Override
    public boolean updateStatusById(Serializable id, DeletedEnum deletedEnum) {
        return super.update(
                new LambdaUpdateWrapper<Role>()
                        .eq(Role::getId, id)
                        .set(Role::getDeleted, deletedEnum.getValue()));
    }

    @Override
    public boolean removeById(Serializable id) {
        if(super.removeById(id)){
            rolePermitService.remove(
                    new LambdaQueryWrapper<RolePermit>()
                            .eq(RolePermit::getRoleId, id));
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean allocationPermit(List<RolePermit> rolePermitList) {
        if (!CollectionUtils.isEmpty(rolePermitList)) {
            if(rolePermitService.saveBatch(rolePermitList)){
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean allocationPermit(Integer roleId, List<RolePermit> rolePermitList) {
        rolePermitService.remove(
                new LambdaQueryWrapper<RolePermit>()
                        .eq(RolePermit::getRoleId, roleId));
        if(this.allocationPermit(rolePermitList)){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}